#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/wait.h>
#include<fcntl.h>

#define DEF_PAGER "/bin/more"

int main(int argc,char **argv)
{
	int           n;
	int           fd[2];
	pid_t         pid;
	char          *pager,*argv0;
	char          line[1024];
	FILE          *fp;

	if(argc != 2)
		printf("usage:a.out <pathname>");
	
	if((fp = fopen(argv[1],"r")) ==NULL )
		printf("can't open %s",argv[1]);
	
	if(pipe(fd) < 0)
		printf("pipe error\n");
	
	if((pid = fork()) < 0)
		printf("fork error\n");
	else if(pid > 0)
	{
		close(fd[0]);
		while(fgets(line,1024,fp) != NULL)
		{
			n = strlen(line);
			if(write(fd[1],line,n) != n)
				printf("write error to pipe\n");
		}
	if(ferror(fp))
		printf("fgets error\n");
	close(fd[1]);
	if(waitpid(pid,NULL,0) < 0)
		printf("waitpid error\n");
	exit(0);
    }

	else
	{
		close(fd[1]);
		if(fd[0] != STDIN_FILENO)
		{
			if(dup2(fd[0],STDIN_FILENO) != STDIN_FILENO)
				printf("dup2 error to stdin\n");
			close(fd[0]);
		}

		if((pager = getenv("PAGER")) == NULL)
			pager = DEF_PAGER;
		if((argv0 = strrchr(pager,'/')) != NULL)
			argv0++;
		else
			argv0 = pager;

        printf("argv0:%s\n",argv0);
        printf("pager:%s\n",pager);

		if(execl(pager,argv0,(char *)0) < 0)
			printf("execl error for %s",pager);
	}

	exit(0);

}
